package com.yh.Dao.Impl;

import com.yh.Dao.IPagesDao;
import com.yh.entity.Bord;
import com.yh.until.DBuntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PagesDaoImpl extends DBuntil implements IPagesDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public List getPage(int pagesize, int pagenum) {
        List list=new ArrayList();
        conn=getConn();
        try {
            String sql="select * from bord limit ?,?";
            pst=conn.prepareStatement(sql);
            pst.setInt(1,pagesize*(pagenum-1));
            pst.setInt(2,pagesize);
            rs=pst.executeQuery();
            while (rs.next()){
                Bord b=new Bord(rs.getInt(1),rs.getString(2));
                list.add(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clossAll(conn,pst,rs);
        }

        return list;
    }

    @Override
    public int getCount() {
        int count=0;
        try {
            conn=getConn();
            String sql=" select count(1) from bord";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            if(rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clossAll(conn,pst,rs);
        }
        return count;
    }
}
